package com.trans.auxiliary;

import com.trans.anno.Info;
import com.trans.anno.Translator;
import com.trans.utils.ParamsUtils;
import lombok.AllArgsConstructor;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xwduo
 * @Description //TODO
 * @Date 2022-10-20 14:36
 */
@AllArgsConstructor
public class HandleFullRecord {

    @Info("注解对象")
    public final Translator translator;

    @Info("字段属性")
    public final Params params;

    @Info("已翻译的对象值")
    public final Map<String,Object> item;

    @Info("当前层级的源对象（eg：）")
    public final Object record;

    @Info("翻译的原始对象（eg：）")
    public final Object fullRecord;

    public HandleRecord toRecord(){
        return new HandleRecord(translator, item, record, params, fullRecord);
    }

    //获取值-当前翻译字段
    public Object getVal(String name){
        return item.get(name);
    }

    //获取值-当前翻译字段
    public Object getVal(){
        return item.get(params.name);
    }

    //获取原始值-当前翻译字段
    public Object getOriginVal(){
        return params.findObjectVal(record);
    }

    //获取原始值
    public Object getOriginVal(String name){
        return ParamsUtils.getObjVal(record,name);
    }

    public void putItemVal(String name,Object obj){
        //这里是防止篡改已有的数据？可能会导致没这么方便？
        if( ! item.containsKey(name)){
            item.put(name,obj);
        }
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        return params.getAnnotation(annotationClass);
    }

    public String getCacheKey(){
        return String.valueOf(record.hashCode()).concat("||").concat(record.getClass().getName());
    }
}
